<?php
namespace app\web\home;

use app\index\controller\Home;
use app\web\model\Banner;
use app\web\model\Category;
use app\web\model\Problem;
use app\web\model\Job;
use app\web\model\Experience;
use app\web\model\Traffic;
use app\web\model\Config;
use app\web\model\Order;
use app\web\model\Base;
use app\web\model\Technician as myModel;
use think\facade\Request;
use think\facade\Cache;
/**
 * 首页\公共接口
 */
class Index extends Home 
{
	/**
     * 首页列表
     * [getList description]
     * @return [type] [description]
     */
    public function getList()
    {
        $order = input('order',0);
        $city = input('city','');
        $where[] = ['a.city','like','%'.$city.'%'];
        $where[] = ['a.is_online','=',1];
        $where[] = ['a.is_sale','=',1];
        $where[] = ['a.is_sale1','=',1];
        $where[] = ['a.check_status','=',1];
        $where[] = ['a.is_period','=',0];
        $where[] = ['a.is_project','=',1];
        if(!empty($params['category_id'])){
            $where[] = ['a.id','in',\app\web\model\ProjectTechnician::getTidByCategoryID($params['category_id'])];
        }
        $product = \app\lib\model\Tool::getProduct(input('longitude'),input('latitude'));
        $list = [];
        if($order == 0){  //红牌推荐 
            $where[] = ['is_recommend','=',1];
            $sql = myModel::alias('a')
                        ->where($where)
                        ->field('a.id,a.nickName,a.shop_mobile,a.shop_id,a.avatarUrl,a.avatar_similarity,a.real_status,a.traffic_id,a.is_recommend,a.product,abs(a.product-'.($product/10000000).') as distance')
                        ->select(false);
            $list = \think\Db::table('('.$sql.')')->alias('a')
                        ->leftjoin('shop b','b.id=a.shop_id')
                        ->leftjoin('traffic c','c.id=a.traffic_id')
                        ->field('a.*,ifnull(b.shop_name,"") as shop_name,ifnull(c.traffic_name,"") as traffic_name')
                        ->order('distance')
                        ->paginate()->each(function($item,$key){
                            $item['avatar_authentication'] = $item['avatar_similarity'] < 70 ? 0 : 1;   //头像是否认证
                            $item['is_mobile'] = $item['shop_mobile'] ? 1 : 0;
                            $coupon_type = \app\web\model\CouponType::where(['coupon_type'=>2,'technician_id'=>$item['id'],'status'=>1])->field('money,full_money')->find();
                            $item['coupon_money'] = $coupon_type['money'] ?? 0;
                            $item['full_money'] = $coupon_type['full_money'] ?? 0;
                            return $item;
                        });
        }elseif($order == 1){ //今日推荐按打卡时间倒叙
            $list = myModel::alias('a')
                            ->leftjoin('shop b','b.id=a.shop_id')
                            ->field('a.id,a.nickName,a.shop_mobile,a.shop_id,a.avatarUrl,a.avatar_similarity,a.real_status,a.traffic_id,a.is_recommend,ifnull(b.shop_name,"") as shop_name,ifnull(c.traffic_name,"") as traffic_name')
                            ->leftjoin('traffic c','c.id=a.traffic_id')
                            ->where($where)
                            ->order('a.sign_time desc')
                            ->paginate()->each(function($item,$key){
                                $item['avatar_authentication'] = $item['avatar_similarity'] < 70 ? 0 : 1;   //头像是否认证
                                $item['is_mobile'] = $item['shop_mobile'] ? 1 : 0;
                                $coupon_type = \app\web\model\CouponType::where(['coupon_type'=>2,'technician_id'=>$item['id'],'status'=>1])->field('money,full_money')->find();
                                $item['coupon_money'] = $coupon_type['money'] ?? 0;
                                $item['full_money'] = $coupon_type['full_money'] ?? 0;
                            });
        }elseif($order == 2){   //近期成交排序 按照后台设置多少天内成交单数来排序
            $deal_dat = Config::getConfig('deal_dat');
            $deal_dat = strtotime(date("Y-m-d",strtotime(-$deal_dat.' day')));
            $where[] = ['d.create_time','>',$deal_dat];
            $list = myModel::alias('a')
                            ->leftjoin('shop b','b.id=a.shop_id')
                            ->leftjoin('order d','d.technician_id=a.id')
                            ->leftjoin('traffic c','c.id=a.traffic_id')
                            ->field('a.id,a.nickName,a.shop_mobile,a.shop_id,a.avatarUrl,a.avatar_similarity,a.real_status,a.traffic_id,a.is_recommend,ifnull(b.shop_name,"") as shop_name,ifnull(c.traffic_name,"") as traffic_name,sum(d.number) as number')
                            ->where($where)
                            ->group('d.technician_id')
                            ->order('number desc')
                            ->paginate()->each(function($item,$key){
                                $item['avatar_authentication'] = $item['avatar_similarity'] < 70 ? 0 : 1;   //头像是否认证
                                $item['is_mobile'] = $item['shop_mobile'] ? 1 : 0;
                                $coupon_type = \app\web\model\CouponType::where(['coupon_type'=>2,'technician_id'=>$item['id'],'status'=>1])->field('money,full_money')->find();
                                $item['coupon_money'] = $coupon_type['money'] ?? 0;
                                $item['full_money'] = $coupon_type['full_money'] ?? 0;
                            });

        }
        return show(200,'技师列表',$list);
    }


	/**
     * 资质列表
     * [getQualificationsAll description]
     * @return [type] [description]
     */
    public function getQualificationsAll()
    {
        $list = Cache::get('getQualificationsAll');
        if(!$list){
            $list = \app\web\model\TechnicianQualifications::alias('a')->leftjoin('job b','b.id=a.job_id')
                        ->where(['a.status'=>1])
                        ->group('a.job_id')
                        ->field('b.id,b.job_name')
                        ->all();
            Cache::set('getQualificationsAll',$list,300);
        }
        
        return show(200,'资质认证列表',$list);
    }
    

    /**
     * 首页推荐项目
     * [getHomeProject description]
     * @return [type] [description]
     */
    public function getHomeProject()
    {
        $where[] = ['a.all_status','=',1];
        $where[] = ['a.status','=',1];
        $where[] = ['a.is_recommend','=',1];
        $list = \app\web\model\Project::alias('a')->where($where)
            ->where('d.is_sale=1 and d.is_sale1=1 and d.is_online=1 and d.is_period=0 and d.is_project=1')
            ->leftJoin('project_technician c','c.project_id=a.id')
            ->leftJoin('technician d','d.id=c.technician_id')
            ->leftjoin('shop b','b.id=a.shop_id')
            ->field('a.id,a.project_name,a.service_time,a.order_number,a.money,a.image,ifnull(b.shop_name,"") as shop_name')
            ->order('a.money')
            ->paginate()->each(function($v,$k){
                $v['image'] = explode(',', $v['image'])[0];
            });
        return show(200,'项目列表',$list);
    }



	/**
	 * 首页轮播
	 * @author MrGuo
	 * @date   2020-11-06
	 * @return [json]     []
	 */
	public function banner() 
	{
		$list = Banner::where("status", 1)
			->field('id,image')
			->order("sort desc,id desc")
			->select();
		return show(200, "轮播", $list);
	}
	public function category($is_ordinary = 0)
	{
		$map[] = ['status','=',1];
		if($is_ordinary == 1) $map[] = ['category_type','=',0];
		$list = Category::getAll($map);
		return show(200,'项目分类',$list);
	}
	public function problem()
	{
		return show(200,'常见问题',Problem::where('status',1)->order('sort desc,id desc')->field('id,problem,answer')->all());
	}
	public function job()
	{
		return show(200,'职称',Job::where('status',1)->order('sort desc,id desc')->field('id,job_name')->all());
	}
	public function experience()
	{
		return show(200,'工作经验',Experience::where('status',1)->order('sort desc,id desc')->field('id,experience_name')->all());
	}
	public function traffic(){
		return show(200,'公里',Traffic::where('status',1)->field('id,traffic_number,traffic_name')->order('traffic_number')->all());
	}
	public function getConfig($key)
	{
		$config = Config::getConfig();
		if(isset($config[$key])) return show(200,'ok',$config[$key]);
		return show(200,'全部',$config);
	}
	public function coordinateToAddress($latitude,$longitude)
	{
		$data = \app\lib\model\Tool::coordinateToAddress($latitude,$longitude);
		$arr = $data['address_component'];
		$arr['rough'] = $data['formatted_addresses']['rough'];
		return show(200,'地址',$arr);
	}


	/**
	 * 定时任务 自动取消订单 自动完成订单 服务开始前N分钟通知技师 5分钟一次
	 * [task description]
	 * @return [type] [description]
	 */
	public function task(){
		//服务开始前N分钟通知技师
		$res = Order::taskSend();
		//自动取消订单
		$res1 = Order::taskCancelOrder();
		//自动确认订单
		$res2 = Order::taskCompleteOrder();
		return show(200,'订单定时任务',[$res,$res1,$res2]);
	}
	/**
	 * 技师N天未打卡自动下线  上午7点一次 下午一次7点一次 
	 * [task1 description]
	 * @return [type] [description]
	 */
	public function task1(){
		//技师N天未打卡自动下线
		$res = Order::offline();
		$res1 = Base::taskTechnician(); 
		$res2 = Base::taskShop(); 
		return show(200,'订单定时任务1',[$res,$res1,$res2]);
	}
}	